package util;

import com.alibaba.fastjson.JSONArray;
import pojo.ConfigPoJo;
import pojo.LinuxAccount;

import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;

/**
 * 读取配置文件
 */
public class ConfigUtil {

    private ConfigUtil() {
    }

    private static ConfigPoJo configPoJo;

    public static ConfigPoJo getConfig() throws Exception {
        Properties properties = new Properties();
        properties.load(ConfigUtil.class.getResourceAsStream("/config.properties"));
        if (configPoJo == null) {
            configPoJo = new ConfigPoJo();
            configPoJo.setResultPath(properties.getProperty("result.path").trim());
            configPoJo.setSysInterfaceExcelPath(properties.getProperty("sys.interface.excel.path").trim());
            configPoJo.setSysIp(properties.getProperty("sys.ip").trim());

            List<LinuxAccount> accountList = JSONArray.parseArray(properties.getProperty("linux.account").trim(), LinuxAccount.class);
            configPoJo.setLinuxList(accountList);

            configPoJo.setDownloadLinuxFile(properties.getProperty("download.linux.file").trim().toLowerCase());
            configPoJo.setLogSavePath(properties.getProperty("log.save.path").trim());

            if (!configPoJo.getLogSavePath().endsWith("/")) {
                configPoJo.setLogSavePath(configPoJo.getLogSavePath() +"/");
            }

            configPoJo.setPostmanNumber(Integer.valueOf(properties.getProperty("postman.number").trim()));

            configPoJo.setDebug(properties.getProperty("debug").trim());

            configPoJo.setFilterTimeOut(properties.getProperty("filter.time.out").trim());

            configPoJo.setExampleMax(Integer.valueOf(properties.getProperty("example.max").trim()));

            configPoJo.setCompa(properties.getProperty("compa").trim());
            configPoJo.setCompaIp(properties.getProperty("compa.ip").trim());
        }

        return configPoJo;
    }
}
